
<template>
  <div class="comSelect-container">
    <Tree
      :options="treeData"
      v-model="innerValue"
      :placeholder="placeholder"
      clearable
      :disabled="disabled"
      v-on="$listeners"
      lastLevel
      lastLevelKey="type"
      lastLevelValue="department"
      :multiple="multiple"
      :field="field"
    >
    </Tree>
  </div>
</template>

<script>
import Tree from "@/components/Myselectxy/Tree";
import { Bus } from "../eventBus.js";
export default {
  name: "depSelectXy",
  components: {
    Tree,
  },
  props: ["value", "disabled", "placeholder", "multiple", "field"],
  model: {
    prop: "value",
    event: "input",
  },
  data() {
    return {
      treeData: [],
      innerValue: this.value,
    };
  },
  mounted() {
    Bus.$on("change", (val, field) => {
      this.change(val, field);
    });
    Bus.$on("changePeople", (val, field) => {
      this.changePeople(val, field);
    });
  },
  watch: {
    innerValue(val) {
      val && this.$emit("change", val);
      Bus.$emit("handleTest", this.field, val);
    },
    value(val) {
      this.innerValue = val;
    },
  },
  created() {
    this.getData();
  },
  methods: {
    async getData() {
      this.treeData = await this.$store.dispatch("generator/getDepTreeXy");
      this.$emit("depSelectXy", this.treeData);
    },
    async change(val, field) {
      if (this.field === field) {
        let treeData = await this.$store.dispatch("base/getPositionTree");
        let arr = treeData[0].children.filter((item) => item.children !== null);
        let list = arr.map((item) =>
          item.children.filter((value) => {
            if (value.children) {
              return value.children.some((i) => i.id === val);
            }
          })
        );
        while (list.some((item) => Array.isArray(item))) {
          list = [].concat(...list);
        }
        this.innerValue = list[0].id;
        let names = this.$store.state.base.myTree;
        this.$set(names, this.field, list[0].fullName);
        this.$store.commit("base/setMyTree", names); // 将回显的部门存入vuex
      }
    },
    async changePeople(val, field) {
      if (this.field === field) {
        let treeData = await this.$store.dispatch("base/getUserTree");
        let arr = treeData[0].children.filter((item) => item.children !== null);
        let list = arr.map((item) =>
          item.children.filter((value) => {
            if (value.children) {
              return value.children.some((i) => i.id === val);
            }
          })
        );
        while (list.some((item) => Array.isArray(item))) {
          list = [].concat(...list);
        }
        this.innerValue = list[0].id;
        let names = this.$store.state.base.myTree;
        this.$set(names, this.field, list[0].fullName);
        this.$store.commit("base/setMyTree", names); // 将回显的部门存入vuex
      }
    },
  },
};
</script>
<style lang="scss">
.comSelect-container {
  .el-select {
    width: 100%;
  }
}
</style>